package com.cat.dataStructure03;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/minimum-penalty-for-a-shop/
 * @create 2025/11/7 18:30
 * @since JDK17
 */

public class Solution16 {
    public int bestClosingTime(String customers) {
        int ans = 0, n = customers.length(), cost = 0, min_cost = 0;
        char[] arr = customers.toCharArray();
        for (int i = 0; i < n; i++) {
            cost += arr[i] == 'Y' ? 1 : 0;
        }
        min_cost = cost;
        for (int i = 0; i < n; i++) {
            if (arr[i] == 'N') {
                cost++;
            } else {    // Y
                cost--;
                if (cost < min_cost) {
                    min_cost = cost;
                    ans = i + 1;
                }
            }
        }

        return ans;
    }
}
